//
//  ZYStatusViewModel.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/13.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

class ZYStatusViewModel: NSObject {
    
    var statusModel:ZYStatusModel?
    
    // 微博等级图片
    var mbrankImage: UIImage?
    
    // 微博认证图片
    var verifiedImage: UIImage?
    
    // 微博来源
    var sinaSourceStr: String?
    // 转发
    var repostsCountStr: String?
    // 评论
    var commentsCountStr: String?
    // 赞
    var attitudesCountStr: String?
    
    //微博时间(计算型属性)
    var sinaTimeStr: String?{
        return NSDate.getSinaDate(statusModel?.created_at ?? "").sinaDateDescription
        
    }
    
// //来自于model ->加工-> 输出给 view
    init(statusModel:ZYStatusModel) {
        super.init()
        self.statusModel = statusModel
        mbrankImage = dealMbrankImage(statusModel.user?.mbrank ?? 0)
        verifiedImage = dealVerifiedImage(statusModel.user?.verified ?? -1)
        
        sinaSourceStr = dealSinaSourceStr(statusModel.source ?? "")
        repostsCountStr = getSinaCountStr(statusModel.reposts_count ?? 0, title: "转发")
        commentsCountStr = getSinaCountStr(statusModel.comments_count ?? 0, title: "评论")
        attitudesCountStr = getSinaCountStr(statusModel.attitudes_count ?? 0, title: "赞")
        
    }
    
}
//MARK: - 处理转发、评论、赞 － 底部视图
extension ZYStatusViewModel {
    func getSinaCountStr(count:Int,title:String) -> String{
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000 {
            return "\(count)"
        }
        else{
            let cFloat = CGFloat(count/1000)/10
            var cFloatStr = "\(cFloat)万"
            if cFloatStr.containsString(".0"){
                cFloatStr = cFloatStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return cFloatStr
        }
    }
}
//MARK: - 微博来源 －逻辑
extension ZYStatusViewModel {
    func dealSinaSourceStr(source:String) -> String{
        if source.containsString("\">"){
            let startRange = source.rangeOfString("\">")
            let endRange = source.rangeOfString("</")
            let result = source.substringWithRange((startRange?.endIndex)!..<(endRange?.startIndex)!)
            return "来自\(result)"
        }
        return "来自哪里fromWhere"
        
    }
}

//MARK: 处理cell中数据的逻辑
extension ZYStatusViewModel{
    // 处理微博等级image/// 会员等级 1-6
    func dealMbrankImage(mbrank: Int) -> UIImage?{
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return UIImage(named: "common_icon_membership")
    }
    
    // 处理微博用户认证image/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    func dealVerifiedImage(verified: Int) -> UIImage?{
        switch verified{
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    
}

